﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dzs.Web.Admin
{
    public partial class shows : Web.Admin.BasePage
    {
        dzs.BLL.spaceinfo bll = new dzs.BLL.spaceinfo();
        dzs.BLL.instore bi = new BLL.instore();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dzs.BLL.storeinfo bst = new BLL.storeinfo();
                ddlstore.DataSource = bst.GetddlList(" uid="+getid());
                ddlstore.DataTextField = "sname";
                ddlstore.DataValueField = "sid";
                ddlstore.DataBind();
                ddlstore.Items.Insert(0, new ListItem("所有", "0"));
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int mid = (Convert.ToInt32(Request.Params["id"]));
                    dzs.BLL.materialinfo bs = new BLL.materialinfo();
                    txtnum.Text = bs.GetNum(mid).ToString();
                    hfnum.Value = txtnum.Text;
                    //BindData();
                }              
            }            
        }
        
        public void BindData()
        {
            int id = getid();
            DataSet ds = new DataSet();  
            int sid=int.Parse(ddlstore.SelectedValue);
            string sql=" and uid="+id;
            if(sid!=0)
            sql+= " and sid=" + sid;
            if (txtname.Text != "")
                sql += string.Format(" and spname like '%{0}%'",txtname.Text);
            ds = bll.GetinspaceList(sql);
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int mid = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                mid = (Convert.ToInt32(Request.Params["id"]));
                int status = int.Parse(ddlstatus.SelectedValue.ToString());
                int uid = 0;//通过登录来取得
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;                    
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                    uid = user_id;
                    int spid = GetSelID();
                    if (spid != 0)
                    {
                        decimal num = Convert.ToDecimal(txtnum.Text);
                        decimal onum = Convert.ToDecimal(hfnum.Value);
                        hfnum.Value = (onum - num).ToString();
                        txtnum.Text = hfnum.Value;
                        int r = bi.inst(mid, spid, uid, num, status);
                        if (r > 0)
                        {
                            if (onum > num)
                            {
                                Maticsoft.Common.MessageBox.Show(this, "本资料还没有完全入库，请继续入库！");
                            }
                            else
                            {   
                                //修改物料状态
                                dzs.BLL.materialinfo bm = new BLL.materialinfo();
                                bm.Updatestatus(mid, 1);
                                int id = bm.Getdid(mid);
                                if (id != 0)
                                {
                                    Maticsoft.BLL.monthSupplayPlan mb = new Maticsoft.BLL.monthSupplayPlan();
                                    mb.Updatedate(2, id);
                                    mb.Updateresult(id);
                                }
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('入库成功！');closedialog();", true);

                            }
                            //修改月供应计划相关状态
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this, "入库失败！");
                        }
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "请选择仓位！");

                    }
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没的指定要入库的物料！');closedialog();", true);

                }
            }
            else
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('没的指定要入库的物料！');closedialog();", true);
            }
        }
        private int GetSelID()
        {
            int id = 0;           
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {                    
                    if (gridView.DataKeys[i].Value != null)
                    {
                        id= int.Parse(gridView.DataKeys[i].Value.ToString());
                        break;
                    }
                }
            }           
            return id;
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}